package cn.wolfcode.shop.cloud.redis;


import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.msg.RedisCodeMsg;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;


public class RedisService {
    @Autowired
    JedisPool jedisPool;

    //set
    public <T> void set(KeyPrefix keyPrefix, String key, T data) {
        try (Jedis jedis = jedisPool.getResource()) {
            //判断是否有过期时间
            String realKey = keyPrefix.getPreix() + key;
            String val = JSON.toJSONString(data);
            if (keyPrefix.getExpireTime() > 0) {
                jedis.setex(realKey, keyPrefix.getExpireTime(), val);
            } else {
                jedis.set(realKey, val);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_OPT);
        }
    }

    //get
    public <T> T get(KeyPrefix keyPrefix, String key, Class<T> clz) {
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getPreix() + key;
            String str = jedis.get(realKey);
            if (StringUtils.isEmpty(str)) {
                return null;
            }
            return JSON.parseObject(str, clz);
        } catch (Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_OPT);
        }
    }
    //incr
    public <T> long incr(KeyPrefix keyPrefix, String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getPreix() + key;
            return jedis.incr(realKey);
        } catch (Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_OPT);
        }
    }
    //decr
    public <T> long decr(KeyPrefix keyPrefix, String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getPreix() + key;
            return jedis.decr(realKey);
        } catch (Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_OPT);
        }
    }
    //exists
    public <T> boolean exists(KeyPrefix keyPrefix, String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getPreix() + key;
            return jedis.exists(realKey);
        } catch (Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_OPT);
        }
    }
    //expire
    public <T> long expire(KeyPrefix keyPrefix, String key,int expreTime) {
        try (Jedis jedis = jedisPool.getResource()) {
            //设置过期时间
            String realKey = keyPrefix.getPreix() + key;
            return jedis.expire(realKey,expreTime);
        } catch (Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_OPT);
        }
    }
    //hset
    public <T>void hset(KeyPrefix keyPrefix, String key,String field,T data) {
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getPreix() + key;
            jedis.hset(realKey,field,JSON.toJSONString(data));
        } catch (Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_OPT);
        }
    }
    //hgetall
    public <T>Map<String,T> hgetAll(KeyPrefix keyPrefix, String key,Class<T> clz) {
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getPreix() + key;
            Map<String, String> stringStringMap = jedis.hgetAll(realKey);
            Map<String,T> resultMap = new HashMap<>();
            for (Map.Entry<String, String> stringStringEntry : stringStringMap.entrySet()) {
                resultMap.put(stringStringEntry.getKey(),JSON.parseObject(stringStringEntry.getValue(),clz));
            }
            return resultMap;
        } catch (Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_OPT);
        }
    }
    //hset
    public <T>T hget(KeyPrefix keyPrefix, String key,String field,Class<T> clz) {
        try (Jedis jedis = jedisPool.getResource()) {
            String realKey = keyPrefix.getPreix() + key;
            return JSON.parseObject(jedis.hget(realKey,field),clz);
        } catch (Exception e) {
            throw new BusinessException(RedisCodeMsg.REDIS_OPT);
        }
    }


}
